home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0411.ZIP / STRIPP.C < prev    next >
Text File  |  1985-05-28  |  3KB  |  139 lines

  1. /*
  2.  * Program to read a file and strip nominated characters from it.
  3.  */
  4. #include "stdio.h"
  5. main(argc,argv)
  6.     int argc;
  7.     char *argv[];
  8.     {
  9.     int  c,
  10.      outfile,
  11.      infile,
  12.      cntr,
  13.      oneflag;
  14.     long value;
  15.     if (argc == 1)
  16.     {
  17.     puts("\n");
  18.     puts("Syntax: STRIPP <infile> <outfile> /nn [/1]\n\n");
  19.     puts("where   infile  is the filespec for the input data\n");
  20.     puts("        outfile is the filespec for the output data\n\n");
  21.     puts("        /nn     is the hex value of the character to\n");
  22.     puts("                be stripped from the file\n");
  23.     puts("        /1      indicates that only the 1st character\n");
  24.     puts("                which meets the specifications should\n");
  25.     puts("                be deleted.\n\n");
  26.     exit(0);
  27.     }
  28.     if (argc == 1)
  29.     {
  30.     puts("Author:  Peter Townsend\n");
  31.     puts("Date:    28May85\n");
  32.     puts("Version: 1.0\n");
  33.     }
  34.     if ((infile = open(argv[1],0)) == -1)
  35.     {
  36.     printf("\nCannot open input file %s\n",argv[1]);
  37.     exit(1);
  38.     }
  39.     if (argc == 2)
  40.     {
  41.     printf("\nMissing Output Filename\n");
  42.     exit(1);
  43.     }
  44.     if (strcmp(argv[1],argv[2]) == 0)
  45.     {
  46.     printf("\nInput filename and output filename cannot be the same.\n");
  47.     exit(1);
  48.     }
  49.     if ((outfile = open(argv[2],1)) == -1)
  50.     {
  51.     if ((outfile = creat(argv[2])) == -1)
  52.         {
  53.         printf("\nCannot open/create output file %s\n",argv[2]);
  54.         exit(1);
  55.         }
  56.     }
  57.     if (argc == 3)
  58.     {
  59.     printf("\nMissing hex character value\n");
  60.     exit(1);
  61.     }
  62.     if ((argv[3][0]) == '/')
  63.     {
  64.     value = 0;
  65.     c = tolower(argv[3][1]);
  66.     if ((c >= '0') && (c <= '9'))
  67.         {
  68.         value = (c - '0') * 16;
  69.         }
  70.     else
  71.         {
  72.         if ((c < 'a') || (c > 'f'))
  73.         {
  74.         printf("\nInvalid hex number %s\n",argv[3]);
  75.         exit(1);
  76.         }
  77.         else
  78.         {
  79.         value = (c - 'a' + 10) * 16;
  80.         }
  81.         }
  82.     c = tolower(argv[3][2]);
  83.     if ((c >= '0') && (c <= '9'))
  84.         {
  85.         value = value + (c - '0');
  86.         }
  87.     else
  88.         {
  89.         if ((c < 'a') || (c > 'f'))
  90.         {
  91.         printf("\nInvalid hex number %s\n",argv[3]);
  92.         exit(1);
  93.         }
  94.         else
  95.         {
  96.         value = value + (c - 'a' + 10);
  97.         }
  98.         }
  99.     }
  100.     else
  101.     {
  102.     puts("\nYou have not specified a (hex) character");
  103.     puts("\nto be stripped from your file.\n\n");
  104.     exit(1);
  105.     }
  106.     if (argc > 4)
  107.     {
  108.     if (strcmp(argv[4],"/1") == 0)
  109.         {
  110.         oneflag = 1;
  111.         }
  112.     else
  113.         {
  114.         oneflag = 0;
  115.         }
  116.     }
  117.     else
  118.     {
  119.     oneflag = 0;
  120.     }
  121.     while ((c = fgetc(infile)) != EOF)
  122.     {
  123.     if (c != value)
  124.         {
  125.         putc(c,outfile);
  126.         }
  127.     else
  128.         {
  129.         if (oneflag == 1)
  130.         {
  131.         value = 256 + value;
  132.         }
  133.         }
  134.     }
  135.     fclose(infile);
  136.     fclose(outfile);
  137.     exit(0);
  138.     }
  139.